<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>this的指向</title>
  </head>
  <body>
    <script>
      //1、构造函数
      function Pet(name) {
        this.name = name;
        this.getName = function () {
          console.log(this);
          return this.name;
        };
      }
      const cat = new Pet("jack");
      console.log(cat.getName()); // jack
      const { getName } = cat;
      console.log(getName()); //window
      //2.普通函数直接调用
      function foo(params) {
        return this;
      }
      const o = { name: "jim", func: foo };
      console.log(foo());
      //3.将函数作为对象的方法调用
      console.log(o.func()); //谁调用就指向谁
    </script>
  </body>
</html>
